package Test1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

public class Test2 {
    static Scanner sc = new Scanner(System.in);
    static JTextField j = new JTextField();
    static JTextField j1 = new JTextField();
    static JTextField j2 = new JTextField();
    static String s1="";
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = "";

        JFrame jf = new JFrame("test");          // 创建窗口
        jf.setSize(600, 300);                       // 设置窗口大小
        jf.setLocationRelativeTo(null);             // 把窗口位置设置到屏幕中心
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel P = new JPanel();
        P.setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();

        JLabel J = new JLabel();

        JLabel J1 = new JLabel();

        JLabel J2 = new JLabel();

        JTextArea JT = new JTextArea();

        panel.setLayout(new FlowLayout());
        panel2.setLayout(new FlowLayout());

        J.setText("please input the N:   ");
        j.setEditable(true);
        j.setColumns(2);
        panel.add(J);
        panel.add(j);

        J1.setText("please input the Q:     ");
        j1.setEditable(true);
        j1.setColumns(2);
        panel.add(J1);
        panel.add(j1);

        J2.setText("please input the brust: ");
        j2.setEditable(true);
        j2.setColumns(10);

        panel.add(J2);
        panel.add(j2);

        JT.setEditable(false);
        JT.setBackground(Color.lightGray);
        JT.setLineWrap(true);
        JT.setMaximumSize( new Dimension(500, 400) );
        Button b = new Button(" run it !");
        b.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                         add(b);
                                    JT.setText(s1);
                                }
        });

        b.setBackground(Color.YELLOW);

        panel2.add(b);

        P.add(panel,BorderLayout.NORTH);
        P.add(panel2,BorderLayout.SOUTH);
        P.add(JT,BorderLayout.CENTER);

        jf.add(P);
        jf.setVisible(true);
    }

    private static void add (Button b) {
        String a = "";


        int N = Integer.valueOf(String.valueOf(j.getText()));

        int Q = Integer.valueOf(String.valueOf(j1.getText()));
        String h = j2.getText();
        String [] h1 = h.split("\\,");
        int[] brust =new int[N];
        for(int i=0;i<N;i++){
            brust[i]=Integer.valueOf(h1[i]);
        }
        for(int i=0;i<N;i++){

            a = i != N - 1 ? a + brust[i] + "," : a + brust[i];
        }
        s1=  "N:" + N + "\n" +
                "Burst time:" + a + "\n" +
                "Q:" + Q + "\n" +
                "Process     Exec. slices (t)              Waiting               Turnaround"+"\n";
        event(N,Q,brust);

    }

    public static void  event(int N, int Q, int[] brust){
        String a1 = "";
        int index = 0,wait = 0;
        int[] trun = new int[N];
        int ans = 0;
        int[] w = new int[N];//wait time
        int[] step = new int[N];
        String[] s = new String[N]; // to sotre the process
        char[] c = new char[N]; // to print the ABCD
        for(int i=0;i<N;i++){
            s[i]="";
        }


        // to know the max value
        for(int i = 1; i < N; i++){
            index = Math.max(brust[i],brust[i-1]);
        }
        if(Q==0){
            for (int i = 0; i <1; i++) {
                for (int j = 0; j < N; j++) {

                    if (brust[j] != 0) {
                        s[j] =s[j] + ans;
                        ans += brust[j];
                        trun[j] = ans;
                        step[j] += brust[j];
                        brust[j] -= brust[j];
                        w[j] = ans - step[j];

                    }
                }

            }
        }else {
            for (int i = 0; i <= index / Q + 1; i++) {
                for (int j = 0; j < N; j++) {

                    if (brust[j] != 0) {
                        s[j] = i != index / Q + 1 ? s[j] + ans + "," : s[j] + ans;
                        trun[j] = ans;
                        ans += Math.min(brust[j], Q);
                        step[j] += Math.min(brust[j], Q);
                        for(int t=0;t<Math.min(brust[j], Q);t++){
                            a1 += (char)(j+65);
                        }
                        brust[j] -= Math.min(brust[j], Q);
                        w[j] = ans - step[j];

                    } else {
                        s[j] = i != index / Q + 1 ? s[j] + "   " + "  " : s[j] + "  ";
                    }
                }

            }
        }
        for(int i:w){
            wait += i;
        }

        wait/=N;

        for(int i=0;i<N;i++){

            c[i]= (char)(i+65);
            s1 += c[i]+"                 " +s[i]+"\t"+w[i]+" \t "+(trun[i]+1)+"\n";
        }

        s1 += "Gantt chart (1 char = 1 microsecond):"+"\n"+a1+"\n";
        s1 += "Avg. waiting time for "+N+" processes = " + wait;


    }
}


